package com.kuangstudy.controller.collect;

import com.kuangstudy.entity.Collect;
import com.kuangstudy.service.collect.ICollectService;
import com.kuangstudy.vo.CollectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * Description:
 * Author: yykk Administrator
 * Version: 1.0
 * Create Date Time: 2021/11/26 23:08.
 * Update Date Time:
 *
 * @see
 */
@Controller
public class CollectController {

    @Autowired
    @Qualifier("collectService")
    private ICollectService collectService;

    @GetMapping(value = {"/", "/index"})
    public ModelAndView collectIndex() {
        // 3: 定义模型视图
        ModelAndView modelAndView = new ModelAndView();
        // 2: 指定跳转的页面
        modelAndView.setViewName("/collect/index");
        return modelAndView;
    }

    /**
     * 归档查询
     *
     * @return
     */
    @GetMapping("/api/collect/load")
    @ResponseBody
    public List<CollectVo> loadCollectVos() {
        return collectService.loadCollectVos();
    }


    /**
     * 根据id查询收藏夹信息
     *
     * @return
     */
    @GetMapping("/api/collect/get/{id}")
    @ResponseBody
    public Collect getCollect(@PathVariable("id") Long id) {
        return collectService.getById(id);
    }


    /**
     * 根据id删除归档
     *
     * @return
     */
    @PostMapping("/api/collect/del/{id}")
    @ResponseBody
    public boolean delCollectById(@PathVariable("id") Long id) {
        return collectService.removeById(id);
    }

    /**
     * 保存和修改收藏夹
     *
     * @return
     */
    @PostMapping("/api/collect/saveupdate")
    @ResponseBody
    public Collect saveupdateCollect(@RequestBody Collect collect) {
        boolean flag = collectService.saveOrUpdate(collect);
        return flag ? collect : null;
    }

}
